/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heuristics;

/**
 *
 * @author Ahmad Zakaria
 */
import java.awt.Toolkit;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import misc.MapGenerator;

public class TestCoverage {

//    public static int numberOfIteration = 500;
    public static int width = 500, height = 500;

    public static void main(String[] args) {
//      MinimumNormCoverage m = new MinimumNormCoverage("map226757943.txt");
//
//        File folder = new File("assets/Scenes/maps");
//        File[] listOfFiles = folder.listFiles();
//        MapGenerator mapGen = new MapGenerator(500, 500);
//        int numpers[] = {10, 20, 30, 40, 50};
//        int mindist[] = {5, 8, 12, 17, 20};
        int numpers[] = {10};
        int mindist[] = {5};
//        for (int z = 0; z < numpers.length; z++) {
//            String dir = "assets/Scenes/maps_" + numpers[z] + "_" + mindist[z];
//            for (int mapIndex = 1; mapIndex < listOfFiles.length; mapIndex++) {
//                mapGen.generateMap(numpers[z], mindist[z], dir, listOfFiles[mapIndex]);
//            }
//        }

        double avgs[] = new double[numpers.length];
        double stddvs[] = new double[numpers.length];
        double stabs[] = new double[numpers.length];
        double stabilityStddv[] = new double[numpers.length];

        for (int k = 0; k < numpers.length; k++) {
            int numPersons = numpers[k];
            int minDistance = mindist[k];
//            System.out.println(numPersons);

            File folder = new File("assets/Scenes/maps_" + numPersons + "_" + minDistance);
            File[] listOfFiles = folder.listFiles();
            listOfFiles = folder.listFiles();
            HeuristicSimulation heu = null;

//            int NUM_TO_RUN = listOfFiles.length - 1;
            int NUM_TO_RUN = 1;
            int numAlgos = 1; // set to number of used algorithms
            ArrayList<Double>[] thetaChanges = new ArrayList[numAlgos];
            double totalAvg[] = new double[numAlgos];
            double percentCovg[] = new double[numAlgos];
            double stdev[] = new double[numAlgos];
            int min[] = new int[numAlgos];
            int max[] = new int[numAlgos];
            double cvg[][];
            double avgStability[] = new double[numAlgos];
            for (int mapIndex = 1; mapIndex < listOfFiles.length; mapIndex++) {
                heu = new HeuristicSimulation(listOfFiles[mapIndex].getAbsolutePath());
                cvg = heu.simulate(mapIndex);

                for (int i = 0; i < numAlgos; i++) {
                    totalAvg[i] += cvg[i][0];
                    stdev[i] += cvg[i][1];
                    min[i] = (int) cvg[i][2];
                    max[i] = (int) cvg[i][3];
                    avgStability[i] += cvg[i][4];
                }
            }

            Toolkit.getDefaultToolkit().beep();
            Toolkit.getDefaultToolkit().beep();
            Toolkit.getDefaultToolkit().beep();
            Toolkit.getDefaultToolkit().beep();

            for (int i = 0; i < numAlgos; i++) {
                totalAvg[i] /= (NUM_TO_RUN * 1.0);
                percentCovg[i] = (totalAvg[i] / (numPersons * 1.0)) * 100;
                stdev[i] /= NUM_TO_RUN;
                avgStability[i] /= NUM_TO_RUN - 1;
                avgs[k] = percentCovg[i];
                stddvs[k] = stdev[i];
                stabs[k] = avgStability[i];

                System.out.println("**** persentCov:");
                for (int j = 0; j < avgs.length; j++) {
                    System.out.println(avgs[j]);
                }
                System.out.println("**** stability:");
                for (int j = 0; j < avgs.length; j++) {
                    System.out.println(stabs[j]);
                }
                System.out.println("=============================");
            }
        }
    }

    private static double computeStdDev(ArrayList<Double> thetaChanges) {
        double s1 = 0;
        double s2 = 0;
        for (int i = 0; i < thetaChanges.size(); i++) {
            s1 += Math.pow(thetaChanges.get(i), 2);
            s2 += thetaChanges.get(i);
        }
        s1 /= thetaChanges.size();
        s2 /= thetaChanges.size();
        s2 = Math.pow(s2, 2);
        return Math.sqrt(s1 - s2);
    }
}
